package com.cff.proxy;

import java.util.Map;

import com.cff.service.HandlerService;

import io.netty.channel.ChannelHandlerContext;

public class HandlerProxy implements HandlerService{
	private ChannelHandlerContext ctx;
	private String msg;
	private HandlerService handlerService;

	public HandlerProxy(ChannelHandlerContext ctx, String msg, Map<String, Object> exportServiceMap) {
		super();
		this.ctx = ctx;
		this.msg = msg;
		handlerService = (HandlerService) exportServiceMap.get("handlerService");
	}

	public ChannelHandlerContext getCtx() {
		return ctx;
	}

	public void setCtx(ChannelHandlerContext ctx) {
		this.ctx = ctx;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}


	@Override
	public void handle() {
		// 收到消息直接打印输出
		System.out.println(ctx.channel().remoteAddress() + " Say : " + msg);
		
		handlerService.handle();
		
		ctx.writeAndFlush("Received your message !\n");
	}

	@Override
	public String handle(String msg) {
		// 收到消息直接打印输出
		System.out.println(ctx.channel().remoteAddress() + " Say : " + msg);
		
		String res = handlerService.handle(msg);
		
		ctx.writeAndFlush(res);
		return null;
	}

}
